package com.wing.system.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.product.model.entity.ProductCategory;
import com.wing.product.model.response.ProductCategoryVO;
import com.wing.system.dao.PropertyCategoryDao;
import com.wing.system.model.entity.PropertyCategory;
import com.wing.system.model.request.PropertyCategoryForm;
import com.wing.system.model.response.PropertyCategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 物料与资产分类-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class PropertyCategoryServiceImpl extends ServiceImpl<PropertyCategoryDao, PropertyCategory> implements PropertyCategoryService {

    @Override
    public int add(PropertyCategoryForm propertyCategoryForm) {

        //校验

        PropertyCategory propertyCategory =new PropertyCategory();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(PropertyCategory.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(propertyCategoryForm, propertyCategory, copyOptions);

        return baseMapper.insert(propertyCategory);
    }

    @Override
    public List<PropertyCategoryVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<PropertyCategoryVO> pageList4app(Page page, Map<String, Object> params) {
        List<PropertyCategoryVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public PropertyCategoryVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public int enable(Long id) {
        PropertyCategoryVO propertyCategoryVO=baseMapper.get4appById(id);
        if (propertyCategoryVO!=null){
            PropertyCategory propertyCategory=new PropertyCategory();
            propertyCategory.setId(id);
            propertyCategory.setIsDeleted(true);
            int updateCount=baseMapper.updateById(propertyCategory);
            PropertyCategory propertyCategoryParent=new PropertyCategory();
            propertyCategoryParent.setId(propertyCategoryVO.getParentId());
            propertyCategoryParent.setHasEnableChild(true);
            baseMapper.updateById(propertyCategoryParent);
            return updateCount;
        }

        return 0;
    }

    @Override
    @Transactional
    public int disable(Long id) {
        PropertyCategoryVO propertyCategoryVO=baseMapper.get4appById(id);
        if (propertyCategoryVO!=null){
            PropertyCategory propertyCategory=new PropertyCategory();
            propertyCategory.setId(id);
            propertyCategory.setIsDeleted(false);
            int updateCount=baseMapper.updateById(propertyCategory);
            int count=baseMapper.selectCount(new QueryWrapper<PropertyCategory>().eq("parent_id",propertyCategoryVO.getParentId()).eq("is_deleted",true));
            if (count==0){
                PropertyCategory propertyCategoryParent=new PropertyCategory();
                propertyCategoryParent.setId(propertyCategoryVO.getParentId());
                propertyCategoryParent.setHasEnableChild(false);
                baseMapper.updateById(propertyCategoryParent);
            }
            return updateCount;
        }

        return 0;
    }
}
